<?php



/**
 * @author       admin
 */
class PhpView {
    protected
        $theme = 'index',
        $layoutPath = null,
        $var = array('key'=>'关键字dehua', 'ds'=>'描述');
    public $pageTitle = '';
    public $webroot = '';
    public $layout = 'layout';
    
    /**
     * 设置布局路径
     *
     * @param string $layoutPath
     */
    public function setLayoutPath($layoutPath) {
        $this->layoutPath = $layoutPath;
    }
    
    public function set($var, $value) {
        $this->var[$var] = $value;
    }
    
    /**
     * @param    string $template    
     * @return   void
     */
    public function render($template, $html=FALSE)
    {
    	$data	= NULL;
        foreach ($this->var as $key => $var) {
            $$key = $var;
        }
        
        ob_start();
        if (!file_exists($template)) {
            exit ('the Template <font color=\'red\'>'. $template . '</font> is not found.');
        }
        include($template);
        
        $content_for_layout = ob_get_clean();
        if ($this->layout) {
            //$layout = ROOT.'/theme/'.$this->theme.'/'.$this->layout.'.php';
           $this->renderLayout($content_for_layout);
        } else {
            echo   $content_for_layout;
        }
        
        if($html){
        	ob_clean();
        }
      	return $data;
    }
    
    protected function renderLayout($content_for_layout) {
        !empty($this->pageTitle) ? $pageTitle = $this->pageTitle : $pageTitle = '';
        if (empty($this->layoutPath)) {
            $layout = ROOT.'/theme/'.$this->theme.'/'.$this->layout.'.php';
        } else {
            $layout = $this->layoutPath.'/'.$this->layout.'.php';
        }
        include($layout);
    }
    
    protected function _render($template) {
        ob_start();
    }
}

